package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;

import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import com.sky.service.DishService;

import com.sky.vo.DishItemVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/admin/dish")
@Api("菜品相关接口")
@Slf4j
public class DishController {
    @Autowired
    private DishService dishService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 新增菜品
     * @param dishDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增菜品")
    @CacheEvict(cacheNames = "dish_",key = "#dishDTO.categoryId")
    public Result insert(@RequestBody DishDTO dishDTO){
        log.info("新增菜品:{}",dishDTO);

        dishService.saveWithFlavor(dishDTO);

        return Result.success();
    }

    /**
     * 菜品分类查询
     * @param dishPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO){
        log.info("菜品分类查询:{}",dishPageQueryDTO);

        PageResult page = dishService.page(dishPageQueryDTO);

        return Result.success(page);
    }

    /**
     * 根据ids批量删除菜品
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("删除菜品")
    @CacheEvict(cacheNames = "dish_",allEntries = true)
    public Result delete(@RequestParam List<Long> ids){
        log.info("删除菜品：{}",ids);

        dishService.delete(ids);

        return Result.success();
    }


    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜品及口味")
    public Result<DishVO> getDishWithFlavorById(@PathVariable Long id){
        log.info("根据id查询菜品及口味:{}",id);

        DishVO dishVO = dishService.getDishWithFlavorById(id);

        return Result.success(dishVO);
    }

    /**
     * 修改菜品
     * @param dishDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改菜品")
    @CacheEvict(cacheNames = "dish_",allEntries = true)
    public Result updateDishWithFlavor(@RequestBody DishDTO dishDTO){
        log.info("修改菜品:{}",dishDTO);

        dishService.updateDishWithFlavor(dishDTO);

        return Result.success();
    }

    /**
     * 菜品起售停售
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("菜品起售停售")
    @CacheEvict(cacheNames = "dish_",allEntries = true)
    public Result startOrStop(@PathVariable int status,Long id){
        log.info("菜品起售停售");

        dishService.startOrStop(status,id);


        return Result.success();
    }

    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<Dish>> getDishByCategoryId(Long categoryId){
        log.info("根据分类id查询菜品");

        List<Dish> dishList = dishService.getDishByCategoryId(categoryId);

        return Result.success(dishList);

    }

}
